package com.shaozi.nc.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.StringUtils;

import com.shaozi.data.cache.redis.CommonQueueRDao;

import redis.clients.jedis.JedisPoolConfig;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Configuration
@AutoConfigureBefore(value = { CommonQueueRDao.class })
public class RedisConfiguration {

	@Value("${reids.prefix.key}")
	private String redisPrefixKeyProp;
	
	@Bean(name = "strRedisTemplate")
	public RedisTemplate strRedisTemplate(@Value("${redis.ip}") String hostName, @Value("${redis.port}") int port,
			@Value("${redis.password}") String password, @Value("${redis.pool.maxIdle}") int maxIdle,
			@Value("${redis.pool.maxTotal}") int maxTotal, @Value("${redis.pool.index}") int index,
			@Value("${redis.pool.maxWaitMillis}") long maxWaitMillis,
			@Value("${redis.pool.testOnBorrow}") boolean testOnBorrow,
			@Value("${redis.pool.testWhileIdle}") boolean testWhileIdle) {
		RedisTemplate template = new RedisTemplate<>();
		template.setConnectionFactory(connectionFactory(hostName, port, password, maxIdle, maxTotal, index,
				maxWaitMillis, testOnBorrow, testWhileIdle));
		template.setKeySerializer(new StringRedisSerializer());
		template.setValueSerializer(new StringRedisSerializer());
		return template;
	}

	@Bean(name = "redisTemplate")
	public RedisTemplate redisTemplate(@Value("${redis.ip}") String hostName, @Value("${redis.port}") int port,
			@Value("${redis.password}") String password, @Value("${redis.pool.maxIdle}") int maxIdle,
			@Value("${redis.pool.maxTotal}") int maxTotal, @Value("${redis.pool.index}") int index,
			@Value("${redis.pool.maxWaitMillis}") long maxWaitMillis,
			@Value("${redis.pool.testOnBorrow}") boolean testOnBorrow,
			@Value("${redis.pool.testWhileIdle}") boolean testWhileIdle) {
		RedisTemplate template = new RedisTemplate<>();
		template.setConnectionFactory(connectionFactory(hostName, port, password, maxIdle, maxTotal, index,
				maxWaitMillis, testOnBorrow, testWhileIdle));
		template.setKeySerializer(new StringRedisSerializer());
//		template.setValueSerializer(new StringRedisSerializer());
		return template;
	}

	public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle,
			int maxTotal, int index, long maxWaitMillis, boolean testOnBorrow, boolean testWhileIdle) {
		JedisConnectionFactory jedis = new JedisConnectionFactory();
		jedis.setHostName(hostName);
		jedis.setPort(port);
		if (!StringUtils.isEmpty(password)) {
			jedis.setPassword(password);
		}
		if (index != 0) {
			jedis.setDatabase(index);
		}
		jedis.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis, testOnBorrow, testWhileIdle));
		// 初始化连接pool
		jedis.afterPropertiesSet();
		RedisConnectionFactory factory = jedis;

		return factory;
	}

	public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow,
			boolean testWhileIdle) {
		JedisPoolConfig poolCofig = new JedisPoolConfig();
		poolCofig.setMaxIdle(maxIdle);
		poolCofig.setMaxTotal(maxTotal);
		poolCofig.setMaxWaitMillis(maxWaitMillis);
		poolCofig.setTestOnBorrow(testOnBorrow);
		poolCofig.setTestWhileIdle(testWhileIdle);
		return poolCofig;
	}

	public String getRedisPrefixKeyProp() {
		return redisPrefixKeyProp;
	}

	public void setRedisPrefixKeyProp(String redisPrefixKeyProp) {
		this.redisPrefixKeyProp = redisPrefixKeyProp;
	}
}
